﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Automation.Protocols.Dao;
using Automation.Protocols.Model;
using Common.Logging;
using SupportLibrary.Message;
using SupportLibrary;

namespace Automation.Dal.Excel.Dao
{
    public class TestDatasetDao: TestDatasetBase
    {
        private ILog log = LogManager.GetLogger(typeof(TestDatasetDao));
         private ExcelProvider _exProvider;
        private string _Path;
        public TestDatasetDao(string path)
        {
            _Path = path;
            _exProvider = ExcelProvider.Create(@path);
            if (_exProvider == null)
                //MessageShow.FileDoesNotExist(path);
                throw new Exception(path + "doesnot exist!");
        }

        public override List<TestDataset> getTestdatasets()
        {
            List<TestDataset> list = new List<TestDataset>();
            //Console.WriteLine("READING SHEET TDataset.");
            log.Debug(Resources.Information.msg_ReadingSheetTDataSet);
            try
            {
                if (_exProvider != null)
                {
                    var TestDatasets = _exProvider.GetSheet<Automation.Dal.Excel.Model.TestDataset>();
                    int i = 0;
                    if (TestDatasets != null)
                    {
                        foreach (Automation.Dal.Excel.Model.TestDataset item in TestDatasets)
                        {
                            if (item.ID != null)
                            {
                                i++;
                                if (item.isValid())
                                    list.Add(new TestDataset() { 
                                        ID = item.ID,
                                        Comment = item.Comment,
                                        Parameter = item.Parameter,
                                        TestCase = item.TestCase,
                                        Values = item.Values
                                    });
                                else
                                {
                                    //MessageShow.InvalidRecord(i + 1);
                                    throw new Exception("Record "+(i+1)+" is invalid!");
                                }
                            }
                        }
                    }
                    log.Debug(Message.ReplaceMessage(Resources.Information.msg_NumofRecord, ByKey.ByNumber(), i.ToString()));
                    //Console.WriteLine(i + " records were read in sheet TDataset.");
                }
            }
            catch (Exception e)
            {
                //MessageShow.ExceptionOccur("Read TDataSet sheet exception.", e);
                log.Error(Resources.ErrorMessage.ReadSheetTDataSetErr + e.GetType().ToString() + ":" + e.Message);
                throw new Exception(Resources.ErrorMessage.ReadSheetTDataSetErr + e.GetType().ToString());
            }
            return list;
        }
    }
}
